package com.google.code.jmarias.client.components;

import com.google.code.jmarias.client.Entity;
import java.util.ArrayList;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Vector2f;
import org.newdawn.slick.state.StateBasedGame;

/**
 * Komponenta obstaravajici animaci vyjeti karet do vejire v hracove ruce
 * 
 * @author bart
 */

public class DealCardsComponent extends AbstractRenderComponent {

    // Uhel pro vykresleni karet (0 -> karty jsou v sobe)
    private float angle = 0f;
    // Pozice karet
    private ArrayList<Vector2f> cardsPositions;

    private ArrayList<Entity> cardsEntities;


    public DealCardsComponent(int id, ArrayList<Vector2f> cardsPositions, ArrayList<Entity> cardsEntities) throws SlickException {
        super(id);
        this.cardsPositions = cardsPositions;
        this.cardsEntities = cardsEntities;
        angle = 0f;
    }

    @Override
    public void update(GameContainer gc, StateBasedGame sb, int delta) {

     if(delta < 500) {
          // Animace vyjeti karet do vejire
          for(int i = 0; i < cardsEntities.size(); i++) {
                 if( cardsEntities.get(i).getPosition().x < cardsPositions.get(i).x ) {
                   if(owner.getPosition().x - 400 * (float) Math.cos(angle * Math.PI/180) > cardsEntities.get(0).getPosition().x) {
                      cardsEntities.get(i).setPosition(new Vector2f(owner.getPosition().x - 400 * (float) Math.cos( angle * Math.PI / 180),
                                                                    owner.getPosition().y - 300 * (float) Math.sin( angle * Math.PI / 180)));
                   }
                 } else {
                   System.out.println(owner.getPosition());
                   cardsEntities.get(i).setPosition(cardsPositions.get(i));
                 }
              }

              // Zvetseni uhlu jak vyjizdi karty do vejire
              if(angle < 180f) {
                  angle = angle + 0.2f * delta;
              } else {
                  // Karty jsou vyjete plnem rozsahu -> zrusim instanci
                  System.out.println("Konec rozdavani");
                  owner.DelComponent(id);
                  return;
              }

      }
    }

    @Override
    public void render(GameContainer gc, StateBasedGame sb, Graphics gr) {
        for(int i = 0; i < cardsEntities.size(); i++) {
            cardsEntities.get(i).render(gc,sb,gr);
        }

    }


}
